package com.huiminpay.merchant.controller;


import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.merchant.covert.PayChannelCovert;
import com.huiminpay.merchant.covert.PayChannelParamCovert;
import com.huiminpay.merchant.covert.PlatformChannelCovert;
import com.huiminpay.merchant.vo.PayChannelParamVo;
import com.huiminpay.merchant.vo.PayChannelVo;
import com.huiminpay.merchant.vo.PlatformChannelVo;
import com.huiminpay.transaction.api.IAppPlatformChannelService;
import com.huiminpay.transaction.api.IPayChannelParamService;
import com.huiminpay.transaction.api.IPayChannelService;
import com.huiminpay.transaction.api.IPlatformChannelService;
import com.huiminpay.transaction.dto.PayChannelDTO;
import com.huiminpay.transaction.dto.PayChannelParamDTO;
import com.huiminpay.transaction.dto.PlatformChannelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "支付渠道参数相关操作",tags = {"支付渠道参数相关操作"},description = "支付渠道参数相关操作包括CRUD")
public class PayChannelController {


    //平台下所有服务的表
    @Reference
    IPlatformChannelService iPlatformChannel;

    //服务和应用的中间表
    @Reference
    IAppPlatformChannelService iAppPlatformChannel;

    //支付渠道表
    @Reference
    IPayChannelService iPayChannel;

    //支付渠道参数表
    @Reference
    IPayChannelParamService iPayChannelParamService;

    @ApiOperation("查询 平台服务 列表")
    @GetMapping("/my/platform-channels")
    public List<PlatformChannelVo> queryPlatformChannels(){

        //查询平台服务类型列表
        List<PlatformChannelDTO> platformChannelDTOS =
                iPlatformChannel.queryPlatformChannels();

        return PlatformChannelCovert.INSTANCE.dto2voList(platformChannelDTOS);
    }


    @ApiOperation("查询应用的 特定服务 是否被开启")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用ID"),
            @ApiImplicitParam(name = "platformChannel",value = "平台服务码")
    })
    @GetMapping("/my/merchants/apps/platformchannels")
    public Integer queryAppBindPlatformChannel(@RequestParam(required = false) String appId,
                                               @RequestParam String platformChannel){
        //校验非空
        if (StringUtil.isEmpty(appId)){
            throw new BusinessException(CommonErrorCode.E_300003);
        }
        if (StringUtil.isEmpty(platformChannel)){
            throw new BusinessException(CommonErrorCode.E_300004);
        }
        Integer i = iAppPlatformChannel.queryAppBindPlatformChannel(appId,platformChannel);

        //返回0表示未绑定，返回1表示已绑定
        return i;
    }


    @ApiOperation("绑定平台服务到应用/为应用开启平台服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用ID"),
            @ApiImplicitParam(name = "platformChannel",value = "平台服务码")
    })
    @PostMapping("/my/apps/{appId}/platform-channels")
    public String bindPlatformChannel2App(@PathVariable String appId,
                                        @RequestParam("platformChannelCodes") String platformChannel){

        //校验非空
        if (StringUtil.isEmpty(appId)){
            throw new BusinessException(CommonErrorCode.E_300003);
        }
        if (StringUtil.isEmpty(platformChannel)){
            throw new BusinessException(CommonErrorCode.E_300004);
        }

        //绑定平台服务到应用
        iAppPlatformChannel.insertAppPlatformChannel(appId,platformChannel);
        return "应用绑定服务成功";

    }


    @ApiOperation("查询当前 服务类型 的 支付通道列表")
    @ApiImplicitParam(name = "platformChannelCode",value = "平台服务码")
    @GetMapping(value = "/my/pay-channels/platform-channel/{platformChannelCode}")
    public List<PayChannelVo> queryAppBindPlatformChannel(
            @PathVariable("platformChannelCode") String platformChannelCode){

        if (StringUtil.isEmpty(platformChannelCode)){
            throw new BusinessException(CommonErrorCode.E_300004);
        }
        List<PayChannelDTO> payChannelDTOS = iPayChannel.queryPayChannels(platformChannelCode);

        List<PayChannelVo> payChannelVos = PayChannelCovert.INSTANCE.dto2voList(payChannelDTOS);
        return payChannelVos;
    }



    @ApiOperation("商户保存 特定应用 特定服务 的 特定支付 渠道的参数")
    @ApiImplicitParam(name = "payChannelParamVo",value = "支付渠道的参数"
            ,dataType = "PayChannelParamVo",paramType = "body")
    @RequestMapping(value = "/my/pay-channel-params",
            method = {RequestMethod.POST,RequestMethod.PUT})
    public String createPayChannelParam(@RequestBody PayChannelParamVo payChannelParamVo) {

        //非空验证
        //应用id
        if (payChannelParamVo.getAppId() == null){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        //平台服务码
        if (payChannelParamVo.getPlatformChannelCode() == null){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        //支付渠道
        if (payChannelParamVo.getPayChannel() == null){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        //支付渠道参数
        if (payChannelParamVo.getParam() == null){
            throw new BusinessException(CommonErrorCode.E_200202);
        }

        //第三方支付渠道名称
        if (StringUtil.isEmpty(payChannelParamVo.getChannelName())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        //取出token里面的merchantID
        payChannelParamVo.setMerchantId(SecurityUtil.getMerchantId());
        //去保存参数
        PayChannelParamDTO payChannelParamDTO = PayChannelParamCovert.INSTANCE.vo2dto(payChannelParamVo);
        iPayChannelParamService.savePayChannelParam(payChannelParamDTO);

        return "操作成功";
    }

    @ApiOperation("查询 特定应用 的 特定平台服务 的 特定支付通道 参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用ID"),
            @ApiImplicitParam(name = "platformChannel",value = "平台服务码"),
            @ApiImplicitParam(name = "payChannel",value = "支付通道")
    })
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public PayChannelParamVo queryPayChannelParam(@PathVariable String appId,
                                                   @PathVariable String platformChannel,
                                                   @PathVariable String payChannel) {
        PayChannelParamDTO payChannelParamDTO =
                iPayChannelParamService.queryPayChannelParam(appId,platformChannel,payChannel);


    return PayChannelParamCovert.INSTANCE.dto2vo(payChannelParamDTO);

    }


    @ApiOperation("查询 特定应用 的 特定平台服务 的支付通道 参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用id"),
            @ApiImplicitParam(name = "platformChannel", value = "服务类型")
    })
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public List<PayChannelParamVo> queryPayChannelParams(@PathVariable String appId,
                                                          @PathVariable String platformChannel) {

        List<PayChannelParamDTO> payChannelParamDTOS =
                iPayChannelParamService.queryPayChannelParams(appId, platformChannel);
        return PayChannelParamCovert.INSTANCE.dto2voList(payChannelParamDTOS);

    }
}
